package com.ruoyi.web.controller.system;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.wb.domain.*;
import com.ruoyi.wb.enmus.PlanExecuteStatusEnums;
import com.ruoyi.wb.enmus.WbAuditType;
import com.ruoyi.wb.service.IWbEntAuthService;
import com.ruoyi.wb.service.IWbMaintainCompanyService;
import com.ruoyi.wb.service.IWbMasterEnterService;
import com.ruoyi.wb.service.IWbPlanService;
import com.ruoyi.wb.vo.WhetherToEnterVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private IWbMasterEnterService  iWbMasterEnterService;

    @Autowired
    private IWbEntAuthService iWbEntAuthService;

    @Autowired
    private IWbPlanService iWbPlanService;

   @Autowired
    private IWbMaintainCompanyService  iWbMaintainCompanyService;



    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid(),loginBody.getIfApp());

        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


    //微信一键登录
    @PostMapping("/loginOpenid")
    @ApiOperation("微信一键登录")
    public AjaxResult loginOpenid(@RequestBody WxLoginVo wxLoginVo){
        return loginService.loginOpenid(wxLoginVo);
    }


    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        //是否入驻
        WbMasterEnter wbMasterEnter = new WbMasterEnter();
        wbMasterEnter.setUserId(   user.getUserId());
        List<WbMasterEnter> wbMasterEnters = iWbMasterEnterService.selectWbMasterEnterList(wbMasterEnter);
       if (!CollectionUtils.isEmpty(wbMasterEnters)){
           for (WbMasterEnter masterEnter : wbMasterEnters) {
               if (masterEnter.getAuditType().equals(WbAuditType.BEENTERING.getCode())){
                   wbMasterEnter=masterEnter;
               }
               break;
           }
       }

        //查询认证企业
        List<WbEntAuth> wbEntAuthList= iWbEntAuthService.selectWbEntAuthListByUserId(user.getUserId());

        AjaxResult ajax = AjaxResult.success();
        WbEntAuth wbEntAuth=null;
        WbMaintainCompany maintainCompany=null;
        if(!CollectionUtils.isEmpty(wbEntAuthList)){
                wbEntAuth = wbEntAuthList.get(0);
            //查询维保公司  企业 和 维保公司关联
            List<WbMaintainCompany> wbMaintainCompanyList=iWbMaintainCompanyService.selectWbMaintainCompanyByAuthId(wbEntAuth.getId());
            if (!CollectionUtils.isEmpty(wbMaintainCompanyList)){
                maintainCompany=wbMaintainCompanyList.get(0);
            }
        }else {
            //查询维保公司  维保人员 与 维保公司关联
            List<WbMaintainCompany> wbMaintainCompanyList=iWbMaintainCompanyService.selectWbMaintainCompanyByUserId(user.getUserId());
            if (!CollectionUtils.isEmpty(wbMaintainCompanyList)){
                maintainCompany=wbMaintainCompanyList.get(0);
            }
        }
        List<WbPlan> wbPlans =null;
        if (!ObjectUtils.isEmpty(wbEntAuth)){
            //查询该企业有没有已经完成的维保任务如果有提醒用户下载
            Long id = wbEntAuth.getId();
            WbPlan wbPlan = new WbPlan();
            wbPlan.setClientId(id);
            wbPlan.setIfDownload(0L);
            wbPlan.setExecStatus(PlanExecuteStatusEnums.MAINTENANCE.getStatus());
            wbPlans = iWbPlanService.selectIfDownLoud(wbPlan);
        }


        WhetherToEnterVo whetherToEnterVo= loginService.whetherToEnter();
        if (whetherToEnterVo.getIfMaster()){
            whetherToEnterVo.setMessage("已经申请师傅入驻，无法在申请其他入驻和维保申请");
        }else if(whetherToEnterVo.getIfEngineer()){
            whetherToEnterVo.setMessage("已经申请工程师入驻，无法在申请其他入驻和维保申请");
        }else if(whetherToEnterVo.getIfOrganization()){
            whetherToEnterVo.setMessage("已经申请机构入驻，无法在申请其他入驻和维保申请");
        }else if(whetherToEnterVo.getIfMaintenance()){
            whetherToEnterVo.setMessage("已经申请维保申请，无法在申请其他入驻");
        }




        ajax.put("user", user);//用户信息
        ajax.put("roles", roles);//角色集合
        ajax.put("permissions", permissions);//权限集合
        ajax.put("wbMasterEnter", wbMasterEnter);//师傅入驻
        ajax.put("wbEntAuth",wbEntAuth);//认证企业
        ajax.put("maintainCompany",maintainCompany);//维保公司
        ajax.put("ifDownload", wbPlans);//是否有未下载
        ajax.put("whetherToEnter",whetherToEnterVo);//是否申请过入驻
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
